package com.likou;

/**
 * Author: 陈桂明
 * Date: 2025/3/28 下午8:26
 * Description: q2
 */
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class q2 {
    public static void main(String[] args) {
        System.out.println(11);
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ans = new ListNode(0);
        ListNode p = ans;
        int carry = 0;

        while (l1 != null && l2 != null) {
            ListNode node = new ListNode((l1.val + l2.val + carry) % 10);
            p.next = node;
            p = p.next;
            carry = (l1.val + l2.val + carry) / 10;
            l1 = l1.next;
            l2 = l2.next;
        }

        if (l1 == null && l2 != null) {
            while (l2 != null) {
                ListNode node = new ListNode((l2.val + carry) % 10);
                p.next = node;
                p = p.next;
                carry = (l2.val + carry) / 10;
                l2 = l2.next;
            }
        }
        if (l2 == null && l1 != null) {
            while (l1 != null) {
                ListNode node = new ListNode((l1.val + carry) % 10);
                p.next = node;
                p = p.next;
                carry = (l1.val + carry) / 10;
                l1 = l1.next;
            }

        }
        if (carry != 0) {
            ListNode node = new ListNode(carry);
            p.next = node;
        }

        return ans.next;

    }


//    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//        ListNode head = null, tail = null;
//        int carry = 0;  // 进位值
//
//        while (l1 != null || l2 != null) {
//            int val1 = l1 != null ? l1.val : 0;
//            int val2 = l2 != null ? l2.val : 0;
//            int sum = val1 + val2 + carry;
//
//            if (head == null) {
//                head = tail = new ListNode(sum % 10);
//            } else {
//                tail.next = new ListNode(sum % 10);
//                tail = tail.next;
//            }
//
//            carry = sum / 10;
//
//            if (l1 != null) {
//                l1 = l1.next;
//            }
//            if (l2 != null) {
//                l2 = l2.next;
//            }
//        }
//        if (carry>0){
//            tail.next = new ListNode(1);
//        }
//
//        return head;
//    }
}
